package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.HttpRequestHelper;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 杨林
 * @create 2022-11-06 18:16 星期日
 * description:
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
//@CrossOrigin  //通过网关解决跨域问题
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){

        //获取参数 (request包含实体类中不含有的参数  因此不用实体类接值)
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //logoData中的空格替换成+  否则图片不能正常显示
        String logoData = (String)paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        hospitalService.save(paramMap);
        return Result.ok();
    }

    /**
     * 根据医院编号查询医院
     */
    @PostMapping("/hospital/show")
    public Result getByHoscode(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)){
            throw new YyghException(20001, "医院编号为空");
        }
        Hospital hospital = hospitalService.getByHoscode(hoscode);

        return Result.ok(hospital);
    }

    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        departmentService.save(paramMap);
        return Result.ok();
    }

    /**
     * 根据条件对department进行分页查询
     */
    @PostMapping("/department/list")
    public Result department(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)paramMap.get("hoscode");
        String page = (String)paramMap.get("page");
        String limit = (String)paramMap.get("limit");

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> pageResult =
                departmentService.selectPage(Integer.parseInt(page), Integer.parseInt(limit), departmentQueryVo);

        return Result.ok(pageResult);
    }

    /**
     * 删除department
     */
    @PostMapping("/department/remove")
    public Result remove(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        scheduleService.save(paramMap);
        return Result.ok();
    }

    @PostMapping("/schedule/list")
    public Result schedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");

        String page = (String)paramMap.get("page");
        String limit = (String)paramMap.get("limit");

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setDepcode(depcode);
        scheduleQueryVo.setHoscode(hoscode);

        Page<Schedule> pageResult =
                scheduleService.selectPage(Integer.parseInt(page), Integer.parseInt(limit), scheduleQueryVo);

        return Result.ok(pageResult);
    }

    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

}
